package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerSample {

    @Slf4j
    static class CounterSample{
        final AtomicInteger runtimes = new AtomicInteger(0);
        void execute(){
            try{
                doBiz();
            }finally {
                afterExecuted();
            }
        }
        void afterExecuted(){
            runtimes.incrementAndGet();
        }
    }

    static class StatusSample{
        final static int STATUS_NEW = 1;
        final static int STATUS_BEFORE_EXEC = 2;
        final static int STATUS_EXEC = 3;
        final static int STATUS_AFTER_EXEC = 4;
        final static int STATUS_AFTER_WAIT_CANCEL = 10;
        final static int STATUS_AFTER_CANCELED = 20;
        AtomicInteger status = new AtomicInteger(STATUS_NEW);
        public void setStatus(int newStatus){
            int currentStatus = status.get();
            for(;;){
                if(currentStatus >= newStatus ){
                    break; //
                }
                if(status.compareAndSet(currentStatus, newStatus)){
                    break;
                }
                currentStatus = status.get();
            }
        }
    }

    private static void doBiz(){
    }
}
